package com.netease.financial.data.net;

import android.content.Context;

import com.netease.financial.data.body.LoginBody;
import com.netease.financial.data.body.PageBody;
import com.netease.financial.data.body.RegisterBody;
import com.netease.financial.data.entity.AccessTokenCheckEntity;
import com.netease.financial.data.entity.ActivityPageEntity;
import com.netease.financial.data.entity.AppUpdateEntity;
import com.netease.financial.data.entity.LaunchPageEntity;
import com.netease.financial.data.entity.LoginEntity;
import com.netease.financial.data.entity.LogoutEntity;
import com.netease.financial.data.entity.PageInfoEntity;
import com.netease.financial.data.entity.RegisterEntity;
import com.netease.financial.data.entity.UnreadMessageCountEntity;
import com.netease.financial.domain.interactor.RegisterInput;

import java.util.List;

import rx.Observable;

/**
 * {@link RestApi} implementation for retrieving data from the network.
 */
public class RestApiImpl implements RestApi {

    private static final String TAG = RestApiImpl.class.getSimpleName();

    private final Context mContext;

    private final NfsRestApiService mNfsRestApiService;

    public RestApiImpl(Context context) {
        mContext = context;
        mNfsRestApiService = ApiServiceProvider.getNfsRestApiService();
    }

    @Override
    public Observable<AppUpdateEntity> getAppUpdateInfo(final String device, final int versionCode) {
        NfsRestApiService service = ApiServiceProvider.getAppUpdateInfoService(mContext, mNfsRestApiService);
        return service.getAppUpdateInfo(device, versionCode);
    }

    @Override
    public Observable<UnreadMessageCountEntity> getUnreadMessageCount(final String accessToken) {
        return mNfsRestApiService.getUnreadMessageCount(accessToken);
    }

    @Override
    public Observable<LoginEntity> login(String clientId, String clientSecret, String appSecret) {
        LoginBody body = new LoginBody();
        body.setClientId(clientId);
        body.setClientSecret(clientSecret);
        return mNfsRestApiService.login(body, appSecret);
    }

    @Override
    public Observable<LogoutEntity> logout(String accessToken, String appSecret) {
        return mNfsRestApiService.logout(accessToken, appSecret);
    }

    @Override
    public Observable<AccessTokenCheckEntity> checkAccessToken(String accessToken, String appSecret) {
        return mNfsRestApiService.checkAccessToken(accessToken, appSecret);
    }

    @Override
    public Observable<ActivityPageEntity> getActivityPageInfo() {
        return mNfsRestApiService.getActivityPageInfo();
    }

    @Override
    public Observable<LaunchPageEntity> getLaunchPageInfo(int widthPixels, int heightPixels, String platform) {
        return mNfsRestApiService.getLaunchPageInfo(widthPixels, heightPixels, platform);
    }

    @Override
    public Observable<RegisterEntity> register(RegisterInput registerInput) {
        RegisterBody body = new RegisterBody();
        body.setMobile(registerInput.getClientId());
        body.setLoginId(registerInput.getLoginId());
        body.setSmsCaptcha(registerInput.getCaptcha());
        body.setEmail(registerInput.getEmail());
        return mNfsRestApiService.register(body, registerInput.getAppSecret());
    }

    @Override
    public Observable<PageInfoEntity> getPageInfo(String pageName, List<String> elements, int widthPixels, int heightPixels, String platform) {
        PageBody.DeviceBody deviceBody = new PageBody.DeviceBody();
        deviceBody.setWidthPixels(widthPixels);
        deviceBody.setHeightPixels(heightPixels);
        deviceBody.setPlatform(platform);

        PageBody pageBody = new PageBody();
        pageBody.setPageName(pageName);
        pageBody.setElements(elements);
        pageBody.setDevice(deviceBody);

        return mNfsRestApiService.getPageInfo(pageBody);
    }

}
